/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */
package ot.s3;

import java.io.*;
import java.util.*;

/**
 *
 * @author Barnet Wagman
 */
public class OpFailedException extends Exception
    implements java.io.Serializable {
    public static final long serialVersionUID = 0L;
    
    public List<Throwable> throwables;
    
    public OpFailedException(String s) { super(s); }
    
    public OpFailedException(Throwable t) { super(t); }
    
    public OpFailedException() {}
    
    public void setThrowableList(List<Throwable> throwables) {
        this.throwables = throwables;
    }
    
        /** 
         * @return the list of <tt>Throwable>/tt>; this may be an
         * empty list.
         *          
         */
    public List<Throwable> getThrowables() {
        if ( throwables != null ) return(throwables);
        else return(new ArrayList<Throwable>());
    }
    
    public String toString() {
     
        String s = super.toString() + "\n" +
                   "OpFailedException throwable list size=" +
                   Integer.toString( (throwables != null)?throwables.size():0 );
        if ( throwables!= null ) {
            for ( Throwable t : throwables ) {
                s += "  " + t.toString() + "\n";
                s += stackTraceToString("  ",t.getStackTrace());                
            }
        }
        return(s);
    }
    
         /** Converts a <tt>StackTrace[]</tt> to a <tt>String</tt>, one
         *  item per line.
         */
    public static String stackTraceToString(String indent,StackTraceElement[] trace) {
        if ( trace == null ) return(indent + "NULL stack trace.");
        String s = "";
        for ( int i = 0; i < trace.length; i++ ) {
            if ( trace[i] != null )
                s += indent + trace[i].toString() + "\n";
            else s += "null trace element at [" + i + "]";
        }
        return(s);
    }  
}